package com.fyzk.activity.ssc;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tyjc.lottery.R;


public class ShiShiCai extends Activity{
	
	//静态常量
	public static final int STA_XING1 = 1;
	public static final int STA_XING2 = 2;
	public static final int STA_XING3 = 3;
	public static final int STA_XING5 = 5;
	public static final int STA_DAXIAO = 6;
	public static final int STA_WF1 = 1;
	public static final int STA_WF2 = 2;
	public static final int STA_WF3 = 3;
	
	//顶部
	private ImageView top_back,top_menu;
	
	//倒计时  期号
	private TextView daojishi;
	
	/**
	 * 倒计时  时间
	 */
	private long timeDaoJiShi = 10*60*1000;
	/**
	 * 另开线程
	 */
	private Handler jiezhiHandler;
	private Runnable jiezhiRun;
	
	//星
	private TextView xing1,xing2,xing3,xing5,daxiao;
	
	private boolean isXing1 = true,isXing2,isXing3,isXing5,isDaxiao;
	
	//玩法
	private LinearLayout wanfa_ll,wanfa2,wanfa3,wanfa5;
	private LinearLayout wf2_zhixuan,wf2_zuxuan,wf2_hezhi,wf3_zx,wf3_z3,wf3_z6,wf5_zx,wf5_tx;
	private ImageView wf2_zhixuan_iv,wf2_zuxuan_iv,wf2_hezhi_iv,wf3_zx_iv,wf3_z3_iv,wf3_z6_iv,wf5_zx_iv,wf5_tx_iv;

	private boolean is_wf2_zhixuan = true,is_wf2_zuxuan,is_wf2_hezhi,is_wf3_zx = true,is_wf3_z3,is_wf3_z6,is_wf5_zx = true,is_wf5_tx;
	
	
	//提示信息
	private TextView tv_tishi1,tv_tishi2;
	private final String ts_xing1_1 = "至少选择一个号码投注，命中开奖号码的个位即中奖！";
	private final String ts_xing1_2 = "奖金：10米";
	private final String ts_xing2_1 = "个、十位各选一个号码投注，按位置分别命中开奖号码的个、十位即中奖！";
	private final String ts_xing2_2 = "奖金：100米";
	private final String ts_xing3_1 = "个、十、百位各选一个或多个号码投注，按位置分别命中开奖号码的个、十、百位即中奖！";
	private final String ts_xing3_2 = "奖金：1000米";
	private final String ts_xing5_1 = "每位各选一个或多个号码投注，按位置全部命中即中奖！";
	private final String ts_xing5_2 = "奖金：100000米";
	private final String ts_daxiao_1 = "每位各选一个号码投注，所选号码与开奖的个位十位号码性质按位置相符即中奖！";
	private final String ts_daxiao_2 = "奖金：4米";
	
	
	//选球区
	private LinearLayout xq_ll;
	private SSC_DrawView xq_draw;
	
	//历史记录
	private LinearLayout lishi;
	
	//已选号
	private TextView yixuan;
	
	
	
	//=======================		方法			=================================	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.shishicai);
		
		info();
		
		
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		
		check_xing();
		
		
		jiezhiHandler  = new Handler();
		
		jiezhiRun = new Runnable() {
			
			@Override
			public void run() {
				
				addTime();
			}
		};
	}
	
	@Override
	protected void onResume() {
		
		super.onResume();
		
		jiezhiHandler.post(jiezhiRun);
	}
	
	@Override
	protected void onPause() {

		jiezhiHandler.removeCallbacks(jiezhiRun);
		
		super.onPause();
	}
	
	/**
	 * 截止时间         开一线程
	 */
	private void addTime(){

		if (timeDaoJiShi != 0) {
			
			jiezhiHandler.postDelayed(jiezhiRun, 1000);
			if (timeDaoJiShi < 0) {
				daojishi.setText("正在获取截止时间...");
			}else {
				
				timeDaoJiShi -= 1000;
				daojishi.setText(""+daojishi(timeDaoJiShi));
			}
			
		}else {
			daojishi.setText("下一期");
			jiezhiHandler.removeCallbacks(jiezhiRun);
			timeDaoJiShi = 10*60*1000;
			jiezhiHandler.post(jiezhiRun);
		}
	}
	
	/**
	 * 初始化控件
	 */
	private void info(){
		
		//顶部
		top_back = (ImageView)findViewById(R.id.ssc_title_iv_back);
		top_menu = (ImageView)findViewById(R.id.ssc_title_iv_popu);
		
		//倒计时
		daojishi = (TextView)findViewById(R.id.ssc_center_time);
		
		
		//星
		xing1 = (TextView)findViewById(R.id.ssc_xing1);
		xing2 = (TextView)findViewById(R.id.ssc_xing2);
		xing3 = (TextView)findViewById(R.id.ssc_xing3);
		xing5 = (TextView)findViewById(R.id.ssc_xing5);
		daxiao = (TextView)findViewById(R.id.ssc_daxiao);
		
		//玩法
		wanfa_ll = (LinearLayout)findViewById(R.id.ssc_wanfa_ll);
		wanfa2 = (LinearLayout)findViewById(R.id.ssc_wanfa2);
		wanfa3 = (LinearLayout)findViewById(R.id.ssc_wanfa3);
		wanfa5 = (LinearLayout)findViewById(R.id.ssc_wanfa5);
		
		wf2_zhixuan = (LinearLayout)findViewById(R.id.ssc_wanfa2_zhixuan);
		wf2_zuxuan = (LinearLayout)findViewById(R.id.ssc_wanfa2_zuxuan);
		wf2_hezhi = (LinearLayout)findViewById(R.id.ssc_wanfa2_hezhi);
		wf3_zx = (LinearLayout)findViewById(R.id.ssc_wanfa3_zhixuan);
		wf3_z3 = (LinearLayout)findViewById(R.id.ssc_wanfa3_zuxuan3);
		wf3_z6 = (LinearLayout)findViewById(R.id.ssc_wanfa3_zuxuan6);
		wf5_zx = (LinearLayout)findViewById(R.id.ssc_wanfa5_zhixuan);
		wf5_tx = (LinearLayout)findViewById(R.id.ssc_wanfa5_tongxuan);
		
		wf2_zhixuan_iv = (ImageView)findViewById(R.id.ssc_wanfa2_iv_zhixuan);
		wf2_zuxuan_iv = (ImageView)findViewById(R.id.ssc_wanfa2_iv_zuxuan);
		wf2_hezhi_iv = (ImageView)findViewById(R.id.ssc_wanfa2_iv_hezhi);
		wf3_zx_iv = (ImageView)findViewById(R.id.ssc_wanfa3_iv_zhixuan);
		wf3_z3_iv = (ImageView)findViewById(R.id.ssc_wanfa3_iv_zuxuan3);
		wf3_z6_iv = (ImageView)findViewById(R.id.ssc_wanfa3_iv_zuxuan6);
		wf5_zx_iv = (ImageView)findViewById(R.id.ssc_wanfa5_iv_zhixuan);
		wf5_tx_iv = (ImageView)findViewById(R.id.ssc_wanfa5_iv_tongxuan);
		
		//提示信息
		tv_tishi1 = (TextView)findViewById(R.id.ssc_tishi1);
		tv_tishi2 = (TextView)findViewById(R.id.ssc_tishi2);
		
		//选球区
		xq_ll = (LinearLayout)findViewById(R.id.ssc_xuanqiu_ll);
		xq_draw = (SSC_DrawView)findViewById(R.id.ssc_xuanqiu_draw);
		
		xq_draw.setXing(1);
		xq_draw.setWanfa(1);
		
		//历史记录
		lishi = (LinearLayout)findViewById(R.id.ssc_lishi);
		addLishi();
		
		//已选号码
		yixuan = (TextView)findViewById(R.id.ssc_yixuan);
		
		//方法
		top_back.setOnClickListener(new SSC_onClick());
		top_menu.setOnClickListener(new SSC_onClick());
		
		xing1.setOnClickListener(new SSC_onClick());
		xing2.setOnClickListener(new SSC_onClick());
		xing3.setOnClickListener(new SSC_onClick());
		xing5.setOnClickListener(new SSC_onClick());
		daxiao.setOnClickListener(new SSC_onClick());
		
		wf2_zhixuan.setOnClickListener(new SSC_onClick());
		wf2_zuxuan.setOnClickListener(new SSC_onClick());
		wf2_hezhi.setOnClickListener(new SSC_onClick());
		wf3_zx.setOnClickListener(new SSC_onClick());
		wf3_z3.setOnClickListener(new SSC_onClick());
		wf3_z6.setOnClickListener(new SSC_onClick());
		wf5_zx.setOnClickListener(new SSC_onClick());
		wf5_tx.setOnClickListener(new SSC_onClick());
		
		
		xq_draw.setTv(yixuan);
		
	}
	
	/**
	 * 星选项卡
	 */
	private void check_xing(){
		if(isXing1){
			xing1.setBackgroundColor(getResources().getColor(R.color.bgh));
			xing2.setBackgroundColor(getResources().getColor(R.color.touming));
			xing3.setBackgroundColor(getResources().getColor(R.color.touming));
			xing5.setBackgroundColor(getResources().getColor(R.color.touming));
			daxiao.setBackgroundColor(getResources().getColor(R.color.touming));
			
			wanfa_ll.setVisibility(View.GONE);
			
			tv_tishi1.setText(ts_xing1_1);
			tv_tishi2.setText(ts_xing1_2);
			
			xq_draw.setXing(STA_XING1);
		}else if(isXing2){
			xing2.setBackgroundColor(getResources().getColor(R.color.bgh));
			xing1.setBackgroundColor(getResources().getColor(R.color.touming));
			xing3.setBackgroundColor(getResources().getColor(R.color.touming));
			xing5.setBackgroundColor(getResources().getColor(R.color.touming));
			daxiao.setBackgroundColor(getResources().getColor(R.color.touming));
			
			wanfa_ll.setVisibility(View.VISIBLE);
			wanfa2.setVisibility(View.VISIBLE);
			wanfa3.setVisibility(View.GONE);
			wanfa5.setVisibility(View.GONE);
			
			tv_tishi1.setText(ts_xing2_1);
			tv_tishi2.setText(ts_xing2_2);
			
			xq_draw.setXing(STA_XING2);
			check_wanfa2();
		}else if(isXing3){
			xing3.setBackgroundColor(getResources().getColor(R.color.bgh));
			xing2.setBackgroundColor(getResources().getColor(R.color.touming));
			xing1.setBackgroundColor(getResources().getColor(R.color.touming));
			xing5.setBackgroundColor(getResources().getColor(R.color.touming));
			daxiao.setBackgroundColor(getResources().getColor(R.color.touming));
			
			wanfa_ll.setVisibility(View.VISIBLE);
			wanfa3.setVisibility(View.VISIBLE);
			wanfa2.setVisibility(View.GONE);
			wanfa5.setVisibility(View.GONE);
			
			tv_tishi1.setText(ts_xing3_1);
			tv_tishi2.setText(ts_xing3_2);
			
			xq_draw.setXing(STA_XING3);
			check_wanfa3();
		}else if(isXing5){
			xing5.setBackgroundColor(getResources().getColor(R.color.bgh));
			xing2.setBackgroundColor(getResources().getColor(R.color.touming));
			xing3.setBackgroundColor(getResources().getColor(R.color.touming));
			xing1.setBackgroundColor(getResources().getColor(R.color.touming));
			daxiao.setBackgroundColor(getResources().getColor(R.color.touming));
			
			wanfa_ll.setVisibility(View.VISIBLE);
			wanfa5.setVisibility(View.VISIBLE);
			wanfa3.setVisibility(View.GONE);
			wanfa2.setVisibility(View.GONE);
			
			tv_tishi1.setText(ts_xing5_1);
			tv_tishi2.setText(ts_xing5_2);
			
			xq_draw.setXing(STA_XING5);
			check_wanfa5();
		}else if(isDaxiao){
			daxiao.setBackgroundColor(getResources().getColor(R.color.bgh));
			xing2.setBackgroundColor(getResources().getColor(R.color.touming));
			xing3.setBackgroundColor(getResources().getColor(R.color.touming));
			xing5.setBackgroundColor(getResources().getColor(R.color.touming));
			xing1.setBackgroundColor(getResources().getColor(R.color.touming));
			
			wanfa_ll.setVisibility(View.GONE);
			
			tv_tishi1.setText(ts_daxiao_1);
			tv_tishi2.setText(ts_daxiao_2);
			
			xq_draw.setXing(STA_DAXIAO);
		}
		
	}
	
	/**
	 * 玩法选项 —— 二星玩法
	 */
	private void check_wanfa2(){
		if(is_wf2_zhixuan){
			wf2_zhixuan_iv.setImageResource(R.drawable.zst_radio_checked);
			wf2_zuxuan_iv.setImageResource(R.drawable.zst_radio_normal);
			wf2_hezhi_iv.setImageResource(R.drawable.zst_radio_normal);
			xq_draw.setWanfa(STA_WF1);
		}else if(is_wf2_zuxuan){
			wf2_zhixuan_iv.setImageResource(R.drawable.zst_radio_normal);
			wf2_zuxuan_iv.setImageResource(R.drawable.zst_radio_checked);
			wf2_hezhi_iv.setImageResource(R.drawable.zst_radio_normal);
			xq_draw.setWanfa(STA_WF2);
		}else{
			wf2_zhixuan_iv.setImageResource(R.drawable.zst_radio_normal);
			wf2_zuxuan_iv.setImageResource(R.drawable.zst_radio_normal);
			wf2_hezhi_iv.setImageResource(R.drawable.zst_radio_checked);
			xq_draw.setWanfa(STA_WF3);
		}
	}
	
	/**
	 * 玩法选项 —— 三星玩法
	 */
	private void check_wanfa3(){
		if(is_wf3_zx){
			wf3_zx_iv.setImageResource(R.drawable.zst_radio_checked);
			wf3_z3_iv.setImageResource(R.drawable.zst_radio_normal);
			wf3_z6_iv.setImageResource(R.drawable.zst_radio_normal);
			xq_draw.setWanfa(STA_WF1);
		}else if(is_wf3_z3){
			wf3_zx_iv.setImageResource(R.drawable.zst_radio_normal);
			wf3_z3_iv.setImageResource(R.drawable.zst_radio_checked);
			wf3_z6_iv.setImageResource(R.drawable.zst_radio_normal);
			xq_draw.setWanfa(STA_WF2);
		}else{
			wf3_zx_iv.setImageResource(R.drawable.zst_radio_normal);
			wf3_z3_iv.setImageResource(R.drawable.zst_radio_normal);
			wf3_z6_iv.setImageResource(R.drawable.zst_radio_checked);
			xq_draw.setWanfa(STA_WF3);
		}
	}
	
	/**
	 * 玩法选项 —— 五星玩法
	 */
	private void check_wanfa5(){
		if(is_wf5_zx){
			wf5_zx_iv.setImageResource(R.drawable.zst_radio_checked);
			wf5_tx_iv.setImageResource(R.drawable.zst_radio_normal);
			xq_draw.setWanfa(STA_WF1);
		}else{
			wf5_zx_iv.setImageResource(R.drawable.zst_radio_normal);
			wf5_tx_iv.setImageResource(R.drawable.zst_radio_checked);
			xq_draw.setWanfa(STA_WF2);
		}
	}
	
	/**
	 * 加载历史记录
	 */
	private void addLishi(){
		for (int i = 0; i < 10; i++) {
			TextView tv = new TextView(this);
			tv.setGravity(Gravity.CENTER);
			tv.setTextColor(Color.BLACK);
			tv.setPadding(10, 8, 10, 8);
			if(i % 2 != 0){
				tv.setBackgroundColor(getResources().getColor(R.color.ssc_lishi_bg));
			}else{
				tv.setBackgroundColor(getResources().getColor(R.color.touming));
			}
			
			tv.setText("20140403-06"+i+"期"+" ----- "+(int)(Math.random()*10)+","
					+(int)(Math.random()*10)+","
					+(int)(Math.random()*10)+","
					+(int)(Math.random()*10)+","
					+(int)(Math.random()*10)+"  "+"小双,大单");
//			tv.getLayoutParams().width = LayoutParams.MATCH_PARENT;
			lishi.addView(tv);
		}
	}
	
	/**
	 * 倒计时
	 * @param l  时间差值
	 * @return 
	 */
	private String daojishi(long l){
		int m,d,h,min,s;
		d = (int) (l/1000/60/60/24);
		h = (int) (l/1000/60/60%24);
		min = (int) (l/1000/60%60);
		s = (int) (l/1000%60);

		String month,date,hour,minutes,seconds;
		seconds = ""+s;
		if (s<10) {
			seconds = "0"+s;
		}
		minutes = ""+min;
		if (min<10) {
			minutes = "0"+min;
		}
		hour = ""+h;
		if (h<10) {
			hour = "0"+h;
		}
		
		return minutes+":"+seconds+"";
	}
	
	
	/**
	 * 单击事件
	 * @author Administrator
	 *
	 */
	class SSC_onClick implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.ssc_title_iv_back:
				ShiShiCai.this.finish();
				break;
			case R.id.ssc_title_iv_popu:
				
				break;
			//星
			case R.id.ssc_xing1:
				isXing1 = true;
				isXing2 = false;
				isXing3 = false;
				isXing5 = false;
				isDaxiao = false;
				
				check_xing();
				break;
			case R.id.ssc_xing2:
				isXing2 = true;
				isXing1 = false;
				isXing3 = false;
				isXing5 = false;
				isDaxiao = false;
				
				check_xing();
				break;
			case R.id.ssc_xing3:
				isXing3 = true;
				isXing2 = false;
				isXing1 = false;
				isXing5 = false;
				isDaxiao = false;
				
				check_xing();
				break;
			case R.id.ssc_xing5:
				isXing5 = true;
				isXing2 = false;
				isXing3 = false;
				isXing1 = false;
				isDaxiao = false;
				
				check_xing();
				break;
			case R.id.ssc_daxiao:
				isDaxiao = true;
				isXing2 = false;
				isXing3 = false;
				isXing5 = false;
				isXing1 = false;
				
				check_xing();
				break;
				
			//玩法
			case R.id.ssc_wanfa2_zhixuan:
				is_wf2_zhixuan = true;
				is_wf2_zuxuan = false;
				is_wf2_hezhi = false;
				
				check_wanfa2();
				break;
			case R.id.ssc_wanfa2_zuxuan:
				is_wf2_zhixuan = false;
				is_wf2_zuxuan = true;
				is_wf2_hezhi = false;
				
				check_wanfa2();
				break;
			case R.id.ssc_wanfa2_hezhi:
				is_wf2_zhixuan = false;
				is_wf2_zuxuan = false;
				is_wf2_hezhi = true;
				
				check_wanfa2();
				break;
			case R.id.ssc_wanfa3_zhixuan:
				is_wf3_zx = true;
				is_wf3_z3 = false;
				is_wf3_z6 = false;
				
				check_wanfa3();
				break;
			case R.id.ssc_wanfa3_zuxuan3:
				is_wf3_zx = false;
				is_wf3_z3 = true;
				is_wf3_z6 = false;
				
				check_wanfa3();
				break;
			case R.id.ssc_wanfa3_zuxuan6:
				is_wf3_zx = false;
				is_wf3_z3 = false;
				is_wf3_z6 = true;
				
				check_wanfa3();
				break;
			case R.id.ssc_wanfa5_zhixuan:
				is_wf5_zx = true;
				is_wf5_tx = false;
				
				check_wanfa5();
				break;
			case R.id.ssc_wanfa5_tongxuan:
				is_wf5_zx = false;
				is_wf5_tx = true;
				
				check_wanfa5();
				break;
				
				
				
			default:
				break;
			}
			
		}
		
	}
}
